package andestours.interfaz;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import andestours.mundo.AndesTours;
import andestours.mundo.DestinoValue;
import andestours.mundo.ServicioValue;
import andestours.mundo.TransporteValue;


public class ServletReserva extends ServletTemplate {

	private static final long serialVersionUID = 1L;
	
	private DestinoValue destino;
	private ServicioValue servicio;
	private TransporteValue transporte;

	private int codigoReserva;

	@Override
	public void imprimirContenido(HttpServletRequest request, PrintWriter out) {
		
		if(request.getParameter("nombre") != null && request.getParameter("tarjeta") != null) {
			
			try {
				Date inicio = new Date();
				codigoReserva = AndesTours.getInstance().reservar(Integer.parseInt(request.getParameter("destino")),
						request.getParameter("nombre"),
						Integer.parseInt(request.getParameter("tarjeta")),
						request.getParameter("servicio"),
						Integer.parseInt(request.getParameter("transporte")),
						request.getParameter("ida"),
						request.getParameter("vuelta"));
				Date fin = new Date();
				System.out.println("RF4 Reservar: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
		} catch (Exception e) {
			out.println("	<tr>");
			out.println("        <td width='35%' bgcolor='#6666aa' valign='baseline'><center> ");
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#FF9966'><b>ERROR</b></font> <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif'>Ha ocurrido el siguiente error:</font><br /><br /><br />");
			out.println("				 <font face='Courier New, Courier, monospace' size='2'>" + e.getMessage() + "<br /><br /><br /> ");
			out.println(" 					<form action='index.html'><input type='submit' value='Volver' /></form>");
			out.println("            </center></td> ");
			out.println("        <td valign='baseline'><center> ");
			out.println("                <br /><img src='logo.jpg' /> ");
			out.println("            </center> ");
			out.println("            </td> ");
			out.println("    </tr> ");
			return;
		}
			
			out.println("    <tr> ");
			out.println("        <td width='35%' bgcolor='#6666aa' valign='baseline'><center> ");
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#FF9966'><b>Reserva Exitosa</b></font><br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                    <font face='Verdana, Arial, Helvetica, sans-serif'>Gracias por escogernos. Su c&oacute;digo de reserva es</font><br /><br />");
			out.println("					<font face='Courier New, Courier, monospace' size='4' color='#FF9966'>" + codigoReserva + "</font><br /> ");
			out.println("                    <br /><br /><font face='Verdana, Arial, Helvetica, sans-serif'>Por favor conserve este n&uacute;mero. Es necesario para efectuar su compra.</font>");
			out.println("                    <br /> ");
			out.println("                    <br /> ");
			out.println("                    <br /> ");
			out.println("              		 <br /> ");
			out.println("                    <form action='compra.html'>");
			out.println("					<input type='hidden' name='reserva' id='reserva' value='" + codigoReserva + "' />");
			out.println("					<input type='submit' value='COMPRAR'/></form>");
			out.println("              		 <br /><br /><br />");
			out.println("                    <form action='index.html'><input type='submit' value='Nueva Reserva'/></form>");
			out.println("            </center></td> ");
			out.println("        <td valign='baseline'><center> ");	
		}
		
		else if(request.getParameter("destino") == null || request.getParameter("servicio") == null || request.getParameter("transporte") == null) {
			out.println("<br /><br /><br />Ha ocurrido un error. Por favor vuelva al inicio e intente de nuevo.");
			return;
		}
		else {
			destino = AndesTours.getInstance().darDestino(Integer.parseInt(request.getParameter("destino")));
			servicio = AndesTours.getInstance().darServicio(request.getParameter("servicio"));
			transporte = AndesTours.getInstance().darTransporte(Integer.parseInt(request.getParameter("transporte")));
			
			out.println("    <tr> ");
			out.println("        <td width='35%' bgcolor='#6666aa' valign='baseline'><center> ");
			out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#FF9966'><b>4. Hacer una reserva</b></font> <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <form action='reserva.html' method='post' onsubmit='return validarReserva()'> ");
			out.println("                    <font face='Verdana, Arial, Helvetica, sans-serif'>Nombre completo</font> <br /> ");
			out.println("                    <input type='text' name='nombre' id='nombre' maxlength='30'/> ");
			out.println("                    <br /> ");
			out.println("                    <br /> ");
			out.println("                    <br /> ");
			out.println("                    <span title='No se preocupe por la seguridad de la informacion, nosotros tampoco lo hacemos.' ");
			out.println(" 				<font face='Verdana, Arial, Helvetica, sans-serif'>N&uacute;mero de tarjeta de cr&eacute;dito</font><br /> ");
			out.println("                    <input type='text' name='tarjeta' id='tarjeta' size='8' maxlength='9' /> ");
			out.println("                    </span> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("                <br /> ");
			out.println("				 	 <input type='hidden' id='destino' name='destino' value='" + destino.getCodigo() + "' />");
			out.println("				 	 <input type='hidden' id='servicio' name='servicio' value='" + servicio.getCodigo() + "' />");
			out.println("				 	 <input type='hidden' id='transporte' name='transporte' value='" + transporte.getCodigo() + "' />");
			out.println("				 	 <input type='hidden' id='ida' name='ida' value='" + request.getParameter("ida") + "' />");
			out.println("				 	 <input type='hidden' id='vuelta' name='vuelta' value='" + request.getParameter("vuelta") + "' />");
			out.println("                    <input type='submit' value='Reservar'/> ");
			out.println("                </form> ");
			out.println("            </center></td> ");
			out.println("        <td valign='baseline'><center> ");
		}
		
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + destino.getNombre() + "</font> <br /> ");
		out.println("                <font face='Times New Roman, Times, serif'>" + destino.getPais() + " - " + destino.getContinente() + "</font> <br /> ");
		out.println("                <img border='2' height='150' src='" + destino.getFoto() + "' /> <br /> ");
		out.println("                <br /><br /> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + servicio.getNombre() + "</font> <br />");
		for(int i=0; i<servicio.getTipo(); i++)
			out.println("&#10029;");
		out.println("				 <br />Restaurante " + servicio.getRestaurante());
		out.println("				 <br /><br /><br />");
		out.println(" 			<font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + transporte.getEmpresa() + "</font> ");
		out.println(" 			<br />Tipo: " + transporte.getTipo());
		out.println(" 			<br />N&uacute;mero: " + transporte.getNum_Viaje());
		
		out.println("		</center>");
		out.println("    </td> ");
		out.println(" </tr> ");
	}

}
